import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';

class AppLocalizations {
  final Locale locale;

  AppLocalizations(this.locale);

  static AppLocalizations of(BuildContext context) {
    return Localizations.of<AppLocalizations>(context, AppLocalizations)!;
  }

  static const _supportLanguage = <String, Map<String, String>>{
    'en': {
      "match": "Events",
      "info": "News",
      "mine": "Mine",
      "home": "Home",
    },
    'zh': {
      "match": "赛事",
      "info": "成都",
      "mine": "我的",
      "home": "首页",
    },
  };

  static List<String> languages() => _supportLanguage.keys.toList();

  String localString(String old) {
    print("language code is $old -- ${locale.languageCode}");
    var source = _supportLanguage[locale.languageCode];
    if (source == null) {
      return old;
    }
    var value = source[old];
    if (value == null) {
      return old;
    }
    return value;
  }
}

class AppLocalizationsDelegate extends LocalizationsDelegate<AppLocalizations> {
  static const AppLocalizationsDelegate delegate = AppLocalizationsDelegate();

  const AppLocalizationsDelegate();

  @override
  bool isSupported(Locale locale) =>
      AppLocalizations.languages().contains(locale.languageCode);

  @override
  Future<AppLocalizations> load(Locale locale) {
    return SynchronousFuture<AppLocalizations>(AppLocalizations(locale));
  }

  @override
  bool shouldReload(AppLocalizationsDelegate old) => false;
}
